﻿using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace EmoneyWeiXin.Controllers
{


    public class JiaQQ2Controller : Controller
    {
        //
        // GET: /JiaQQ/
        const string qqkeymmKey = "QQKey_mm_key";
        public static object objQQkey = new object();
        public static string currentKey = "";
        public ActionResult get99qq()
        {
            //MemcachedTools1 mmtoos = new MemcachedTools1();
            // string qqKey = mmtoos.Get<string>(qqkeymmKey);
            //if (qqKey == null)
            //{
            //    lock (objQQkey)
            //    {
            //            qqKey = QQKeyBLL.GetQiyongKey();
            //            SetQQkeyToMmcache(qqKey);
            //    }
            //}
            //string url = Config.JiaQQQunUrl;
            //return Redirect(string.Format(url, qqKey));
            try
            {
                string ip = Common.GetClientIp();
                string type = "8yuehuodong";
                ThreadPool.QueueUserWorkItem(QQKeyBLL2.InsertJiLu,new JiaQQJiluEntity(){  ClientIP=ip, Type=type});
            }
            catch (Exception )
            {
            }
            if (string.IsNullOrEmpty(currentKey))
            {
                lock (objQQkey)
                {
                    currentKey = QQKeyBLL2.GetQiyongKey();
                }
            }

            string url = Config.JiaQQQunUrl;
            return Redirect(string.Format(url, currentKey));
        }




        public ActionResult SetQQKeyPage()
        {
            return View();
        }

        private void SetQQkeyToMmcache(string qqkey)
        {
            // MemcachedTools1 mmtoos = new MemcachedTools1();
            // mmtoos.Remove(qqkeymmKey);
            //mmtoos.Add(qqkeymmKey, qqkey, 24 * 5, 0, 0);
            currentKey = qqkey;
        }

        public ActionResult AddQQKey()
        {
            string name = Request["QQName"];
            string key = Request["QQkey"];
            try
            {
                QQKeyBLL2.Insert(name, key);
                return Content("添加正确");
            }
            catch (Exception ex)
            {
                return Content("添加失败:" + ex.Message);
            }
        }

        public ActionResult Delete()
        {
            string key = Request["qqKey"];
            try
            {
                QQKeyBLL2.Delete(key);
                return Content("删除成功");
            }
            catch (Exception)
            {
                return Content("删除失败");
            }
        }

        public ActionResult SetStatusTO1()
        {
            string key = Request["qqKey"];
            try
            {
                QQKeyBLL2.SetStatusTo1(key);
                SetQQkeyToMmcache(key);
                return Content("启用成功");

            }
            catch (Exception ex)
            {
                return Content("启用失败:" + ex.Message);
            }
        }

        private int pageSize = 15;
        public ActionResult GetDataByPage()
        {
            string pageIndex = Request["pageIndex"];
            try
            {
                int totalCount = 0;
                DataTable dt = QQKeyBLL2.SelectByPage(pageSize, int.Parse(pageIndex), out totalCount);
                string jsons = JsonTools.ToJson(new { list = dt, totalCount = totalCount });
                return Content(jsons);
            }
            catch (Exception)
            {
                return Content("");
            }
        }




    }
}
